<!-- 部门管理 -->
<template>
  <div>
    <el-form :inline="true">
      <el-form-item label="部门名称">
        <el-input v-model="searchMap.name" placeholder="请输入"></el-input>
      </el-form-item>

      <el-form-item label="部门编号">
        <el-input v-model="searchMap.code" placeholder="请输入"></el-input>
      </el-form-item>

      <el-form-item label="部门类型">
        <el-select v-model="searchMap.departType" clearable placeholder="请选择">
          <el-option v-for="item in typeList" :key="item.value" :label="item.lable" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-button type="primary" @click="fetchData()">查询</el-button>
      <el-button type="primary" @click="handleEdit('')">新增</el-button>
    </el-form>

    <el-table :data="tableData" border stripe style="width: 100%">
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="id" label="序号"> </el-table-column>
      <el-table-column prop="name" label="部门名字"> </el-table-column>
      <el-table-column prop="code" label="部门编码"> </el-table-column>
      <el-table-column prop="departType" label="部门类型">
        <template scope="scope">
          <span v-if="scope.row.departType === 1">部门</span>
          <span v-if="scope.row.departType === 2">小组</span>
          <span v-if="scope.row.departType === 3">项目组</span>
          <span v-if="scope.row.departType === 4">事业部</span>
          <span v-if="scope.row.departType === 5">区域</span>
          <span v-if="scope.row.departType === 6">办公室</span>
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注"></el-table-column>
      <el-table-column prop="status" label="部门状态">
        <template scope="scope">
          <el-button type="primary" v-if="scope.row.status === 0" @click="updateStatus(scope.row.id,scope.row.status)">启用</el-button>
          <el-button type="warning" v-if="scope.row.status === 1" @click="updateStatus(scope.row.id,scope.row.status)">禁止</el-button>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-button @click="handleEdit(scope.row.id)" type="text" size="small">修改</el-button>
          <el-button @click="handleDelete(scope.row.id)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页-->
    <el-pagination @size-change="sizeData" @current-change="pageData" :page-sizes="[5, 10, 20, 30, 60]" :page-size="5"
      layout="total, sizes, prev, pager, next, jumper" :total="count">
    </el-pagination>

    <el-dialog title="编辑" :visible.sync="dialogFormVisible">
      <el-form label-width="80px">
        <el-row>
          <el-col span="11">
            <el-form-item label="上级分类">
              <el-select v-model="depart.parentId">
                <el-option v-for="item in list" :key="item.id" :label="item.name" :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col span="11">
            <el-form-item label="部门类型">
              <el-select v-model="depart.departType">
                <el-option v-for="item in typeList" :key="item.value" :label="item.lable"
                  :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col span="11">
            <el-form-item label="部门名称"><el-input v-model="depart.name" /></el-form-item>
          </el-col>
          <el-col span="11">
            <el-form-item label="部门编号"><el-input v-model="depart.code" /></el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col span="11">
            <el-form-item label="备注"><el-input type="textarea" v-model="depart.remark" /></el-form-item>
          </el-col>
          <el-col span="11">
            <el-form-item label="是否启用">
              <el-radio-group v-model="depart.status">
                <el-radio :label="0">启用</el-radio>
                <el-radio :label="1">禁用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item>
          <el-button type="primary" @click="handleSave()" style="margin-left: 240px">保存</el-button>
          <el-button @click="dialogFormVisible = false" style="margin-left: 10px">关闭</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import departmentService from "@/api/depart";

export default {
  data() {
    return {
      tableData: [],
      typeList: [
        {
          lable: "部门",
          value: 1,
        },
        {
          lable: "小组",
          value: 2,
        },
        {
          lable: "项目组",
          value: 3,
        },
        {
          lable: "事业部",
          value: 4,
        },
        {
          lable: "区域",
          value: 5,
        },
        {
          lable: "办公室",
          value: 6,
        },
      ],
      list:[],
      statusList:{status:'',id:''},
      dialogFormVisible: false,
      id: "",
      depart: {},
      searchMap: {
        limit: 5,
        page: 1,
        name: "",
        code: "",
        departType: "",
      },
      //记录总数
      count: 0,
    };
  },
  //钩子函数
  created() {
    this.findAll();
    this.fetchData();
  },
  methods: {
  
    //修改或新增
    handleEdit(id) {

      this.id=id;

      //打开弹窗
      this.dialogFormVisible = true;

      if(id !== ''){

        //根据id查询数据绑定到模型上面
        departmentService.find(id).then(response => {

          //回显
          this.depart = response.data

          //回显图片
          // this.imageUrl = this.pojo.image

        })

      } else {

        //清除之前模型的脏数据
        this.depart = {

        }

        this.imageUrl = ''
      }
    },

    //新增和修改的提交
    handleSave() {
      departmentService.update(this.id, this.depart).then((response) => {
        //新增或者修改成功
        this.$message({
          message: response.msg,
          type: "success",
        });

        //重新加载列表的数据
        this.fetchData();

        //关闭弹窗
        this.dialogFormVisible = false;
      });
    },

    //查询数据
    fetchData() {
      departmentService.list(this.searchMap).then((response) => {
        this.tableData = response.data;
        this.count = response.count;
      });
    },
    //当页面大小改变的时候调用此方法，传递改变的页面大小
    sizeData(size) {
      this.searchMap.limit = size;
      this.fetchData();
    },
    pageData(page) {
      this.searchMap.page = page;
      this.fetchData();
    },
    

    //查询所有部门
    findAll(){
      departmentService.findAll().then(response=>{
        this.list=response.data
      })
    },

    //点击修改状态
    updateStatus(id,status) {
      this.$confirm('您确定要修改状态吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        if (status === 0) {
            status = 1
          } else {
            status = 0
          }
        this.statusList.status=status
        this.statusList.id=id
        departmentService.update(id,this.statusList).then(response => {
          this.$message({
            type: 'success',
            message: '修改状态成功!'
          });

          this.fetchData()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消修改'
          });
        });
      })

    },
          

    //删除
    handleDelete(id){
      this.$confirm('您确定要删除吗, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {

          departmentService.remove(id).then(response=>{
            this.$message({
              type: 'success',
              message: '删除成功!'
            });

            this.fetchData()

          })
        
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    }
  },
};
</script>
